﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class PipingSJAndPYALL : IExternalCommand
	{
		private bool hasElbow(Element elem)
		{
			bool result = true;
			if (elem is Pipe)
			{
				RoutingPreferenceManager routingPreferenceManager = (elem as Pipe).PipeType.RoutingPreferenceManager;
				try
				{
					if (!(elem.Document.GetElementById((elem as Pipe).PipeType.RoutingPreferenceManager.GetRule((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)1, 0).MEPPartId) is FamilySymbol))
					{
						result = false;
					}
					return result;
				}
				catch
				{
					return false;
				}
			}
			if (elem is Duct)
			{
				RoutingPreferenceManager routingPreferenceManager2 = (elem as Duct).DuctType.RoutingPreferenceManager;
				try
				{
					if (!(elem.Document.GetElementById((elem as Duct).DuctType.RoutingPreferenceManager.GetRule((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)1, 0).MEPPartId) is FamilySymbol))
					{
						result = false;
					}
					return result;
				}
				catch
				{
					return false;
				}
			}
			if (elem is Conduit)
			{
				if (!(elem.Document.GetElementById(elem.Document.GetElementById((elem as Conduit).get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM).AsElementId()).get_Parameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_BEND_PARAM).AsElementId()) is FamilySymbol))
				{
					result = false;
				}
			}
			else if (elem is CableTray && !(elem.Document.GetElementById(elem.Document.GetElementById((elem as CableTray).get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM).AsElementId()).get_Parameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_HORIZONTAL_BEND_PARAM).AsElementId()) is FamilySymbol))
			{
				result = false;
			}
			return result;
		}

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			if (commandData == null)
			{
				throw new NotImplementedException();
			}
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Selection selection = activeUIDocument.Selection;
			List<ViewType> viewTypes = YJKRevitTools.ListViewNeed(2);
			if (!YJKRevitTools.ViewCheck(activeUIDocument, viewTypes))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Result result;
			try
			{
				string text = "升降偏移";
				transaction = new Transaction(document, text);
				transaction.Start();
				RevitVersionFuncs.ClearSelection(selection);
				ISelectionFilter selectionFilter = new PipingSJPYSelectionFilter();
				FormRoundBeamUseHand formRoundBeamUseHand = new FormRoundBeamUseHand(Math.PI*.5);
				if (formRoundBeamUseHand.ShowDialog() == DialogResult.OK)
				{
					double distance = formRoundBeamUseHand.distance;
					Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "请选择要升降或者偏移的风管，水管，线管或桥架,点击点为切割位置点");
					Element element = document.GetElement(reference);
					if (this.hasElbow(element))
					{
						if (element is Pipe)
						{
							ISelectionFilter pipeFilter = new PipeSelectionFilter();
							this.pipePYSJ(reference, formRoundBeamUseHand, document, selection, pipeFilter);
						}
						else if (element is Duct)
						{
							ISelectionFilter pipeFilter2 = new DuctSelectionFilter();
							this.ductPYSJ(reference, formRoundBeamUseHand, document, selection, pipeFilter2);
						}
						else if (element is Conduit)
						{
							ISelectionFilter pipeFilter3 = new ConduitSelectionFilter();
							this.conduitPYSJ(reference, formRoundBeamUseHand, document, selection, pipeFilter3);
						}
						else if (element is CableTray)
						{
							ISelectionFilter pipeFilter4 = new CableTraySelectFilter();
							this.cableTrayPYSJ(reference, formRoundBeamUseHand, document, selection, pipeFilter4);
						}
					}
					else
					{
						YJKMessageBox.Show("请先在管道连接配置中弯头一栏添加弯头族！");
					}
				}
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (1 == (int)transaction.GetStatus())
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		private void danceXUNHUAN(double ddd, YJKLine hyline, XYZ xyz1, XYZ d, FormRoundBeamUseHand form, Document doc, Selection sel)
		{
			ISelectionFilter selectionFilter = new PipingSJPYSelectionFilter();
			Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "请选择需自动调整的平行管道");
			if (doc.GetElement(reference) is Conduit)
			{
				Conduit conduit = doc.GetElement(reference) as Conduit;
				HYConduit hyconduit = new HYConduit(conduit);
				YJKLine hyline2 = new YJKLine(hyconduit.CustomConnectorStart.XYZ, hyconduit.CustomConnectorEnd.XYZ);
				XYZ xyz2 = hyconduit.GetProject(xyz1, true);
				XYZ project = hyconduit.GetProject(d, true);
				if (!hyline2.IsParallel(hyline.Line))
				{
					YJKMessageBox.Show("该线管不平行！");
					return;
				}
				if (form.SJORPY == "偏移")
				{
					HYCreatePipeHelper hycreatePipeHelper = new HYCreatePipeHelper();
					form.distance = ddd;
					double num = hyline.Line.Distance(xyz2);
					if (ddd > 0.0)
					{
						if (hycreatePipeHelper.judgePointIsAnotherLine(hyline.Line, hyconduit.CustomConnectorStart.XYZ))
						{
							xyz2 += (xyz2 - project).Normalize() * num;
						}
						else
						{
							xyz2 -= (xyz2 - project).Normalize() * num;
						}
					}
					else if (hycreatePipeHelper.judgePointIsAnotherLine(hyline.Line, hyconduit.CustomConnectorStart.XYZ))
					{
						xyz2 -= (xyz2 - project).Normalize() * num;
					}
					else
					{
						xyz2 += (xyz2 - project).Normalize() * num;
					}
				}
				if (hyconduit.IsPointOnDuctCurve(xyz2) && hyconduit.IsPointOnDuctCurve(project))
				{
					new HYCableTrayRoundBeam().NewPipePIANYI(conduit, xyz2, form.distance, form.angle, form.SJORPY, project);
					return;
				}
				YJKMessageBox.Show("切割点不在该线管上，无法复制升降或偏移！");
				return;
			}
			else if (doc.GetElement(reference) is CableTray)
			{
				CableTray cableTray = doc.GetElement(reference) as CableTray;
				HYCableTray hycableTray = new HYCableTray(cableTray);
				YJKLine hyline3 = new YJKLine(hycableTray.CustomConnectorStart.XYZ, hycableTray.CustomConnectorEnd.XYZ);
				XYZ xyz3 = hycableTray.GetProject(xyz1, true);
				XYZ project2 = hycableTray.GetProject(d, true);
				if (!hyline3.IsParallel(hyline.Line))
				{
					YJKMessageBox.Show("该桥架不平行！");
					return;
				}
				if (form.SJORPY == "偏移")
				{
					HYCreatePipeHelper hycreatePipeHelper2 = new HYCreatePipeHelper();
					form.distance = ddd;
					double num2 = hyline.Line.Distance(xyz3);
					if (ddd > 0.0)
					{
						if (hycreatePipeHelper2.judgePointIsAnotherLine(hyline.Line, hycableTray.CustomConnectorStart.XYZ))
						{
							xyz3 += (xyz3 - project2).Normalize() * num2;
						}
						else
						{
							xyz3 -= (xyz3 - project2).Normalize() * num2;
						}
					}
					else if (hycreatePipeHelper2.judgePointIsAnotherLine(hyline.Line, hycableTray.CustomConnectorStart.XYZ))
					{
						xyz3 -= (xyz3 - project2).Normalize() * num2;
					}
					else
					{
						xyz3 += (xyz3 - project2).Normalize() * num2;
					}
				}
				if (hycableTray.IsPointOnDuctCurve(xyz3) && hycableTray.IsPointOnDuctCurve(project2))
				{
					new HYCableTrayRoundBeam().NewPipePIANYI(cableTray, xyz3, form.distance, form.angle, form.SJORPY, project2);
					return;
				}
				YJKMessageBox.Show("切割点不在该桥架上，无法复制升降或偏移！");
				return;
			}
			else
			{
				if (!(doc.GetElement(reference) is Duct))
				{
					if (doc.GetElement(reference) is Pipe)
					{
						Pipe pipe = doc.GetElement(reference) as Pipe;
						YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
						YJKLine hyline4 = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
						XYZ xyz4 = hypiping.GetProject(xyz1, true);
						XYZ project3 = hypiping.GetProject(d, true);
						if (hyline4.IsParallel(hyline.Line))
						{
							if (form.SJORPY == "偏移")
							{
								HYCreatePipeHelper hycreatePipeHelper3 = new HYCreatePipeHelper();
								form.distance = ddd;
								double num3 = hyline.Line.Distance(xyz4);
								if (ddd > 0.0)
								{
									if (hycreatePipeHelper3.judgePointIsAnotherLine(hyline.Line, hypiping.StartConnector.Origin))
									{
										xyz4 += (xyz4 - project3).Normalize() * num3;
									}
									else
									{
										xyz4 -= (xyz4 - project3).Normalize() * num3;
									}
								}
								else if (hycreatePipeHelper3.judgePointIsAnotherLine(hyline.Line, hypiping.StartConnector.Origin))
								{
									xyz4 -= (xyz4 - project3).Normalize() * num3;
								}
								else
								{
									xyz4 += (xyz4 - project3).Normalize() * num3;
								}
							}
							if (hypiping.IsPointOnPipingCurve(xyz4) && hypiping.IsPointOnPipingCurve(project3))
							{
								new HYPipeConnect().NewPipePIANYI(pipe, xyz4, form.distance, form.angle, form.SJORPY, project3);
								return;
							}
							YJKMessageBox.Show("切割点不在该管道上，无法复制升降或偏移！");
							return;
						}
						else
						{
							YJKMessageBox.Show("该管道不平行！");
						}
					}
					return;
				}
				Duct duct = doc.GetElement(reference) as Duct;
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct);
				YJKLine hyline5 = new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
				XYZ xyz5 = hypiping2.GetProject(xyz1, true);
				XYZ project4 = hypiping2.GetProject(d, true);
				if (!hyline5.IsParallel(hyline.Line))
				{
					YJKMessageBox.Show("该风管不平行！");
					return;
				}
				if (form.SJORPY == "偏移")
				{
					HYCreatePipeHelper hycreatePipeHelper4 = new HYCreatePipeHelper();
					form.distance = ddd;
					double num4 = hyline.Line.Distance(xyz5);
					if (ddd > 0.0)
					{
						if (hycreatePipeHelper4.judgePointIsAnotherLine(hyline.Line, hypiping2.StartConnector.Origin))
						{
							xyz5 += (xyz5 - project4).Normalize() * num4;
						}
						else
						{
							xyz5 -= (xyz5 - project4).Normalize() * num4;
						}
					}
					else if (hycreatePipeHelper4.judgePointIsAnotherLine(hyline.Line, hypiping2.StartConnector.Origin))
					{
						xyz5 -= (xyz5 - project4).Normalize() * num4;
					}
					else
					{
						xyz5 += (xyz5 - project4).Normalize() * num4;
					}
				}
				if (hypiping2.IsPointOnPipingCurve(xyz5) && hypiping2.IsPointOnPipingCurve(project4))
				{
					new HYDuctAlignment().NewPipePIANYI(duct, xyz5, form.distance, form.angle, form.SJORPY, project4);
					return;
				}
				YJKMessageBox.Show("切割点不在该风管上，无法复制升降或偏移！");
				return;
			}
		}

		private void liangceXUNHUAN(double ddd, YJKLine hyline, XYZ xyz1, XYZ xyz2, FormRoundBeamUseHand form, Document doc, Selection sel)
		{
			ISelectionFilter selectionFilter = new PipingSJPYSelectionFilter();
			Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "请选择需自动调整的平行管道");
			if (doc.GetElement(reference) is Conduit)
			{
				Conduit conduit = doc.GetElement(reference) as Conduit;
				HYConduit hyconduit = new HYConduit(conduit);
				YJKLine hyline2 = new YJKLine(hyconduit.CustomConnectorStart.XYZ, hyconduit.CustomConnectorEnd.XYZ);
				XYZ xyz3 = hyconduit.GetProject(xyz1, true);
				XYZ xyz4 = hyconduit.GetProject(xyz2, true);
				if (!hyline.IsParallel(hyline2.Line))
				{
					YJKMessageBox.Show("该线管不平行！");
					return;
				}
				if (form.SJORPY == "偏移")
				{
					HYCreatePipeHelper hycreatePipeHelper = new HYCreatePipeHelper();
					form.distance = ddd;
					double num = hyline.Line.Distance(xyz3);
					if (ddd > 0.0)
					{
						if (hycreatePipeHelper.judgePointIsAnotherLine(hyline.Line, hyconduit.CustomConnectorStart.XYZ))
						{
							xyz3 += (xyz3 - xyz4).Normalize() * num;
							xyz4 += (xyz4 - xyz3).Normalize() * num;
						}
						else
						{
							xyz3 -= (xyz3 - xyz4).Normalize() * num;
							xyz4 -= (xyz4 - xyz3).Normalize() * num;
						}
					}
					else if (hycreatePipeHelper.judgePointIsAnotherLine(hyline.Line, hyconduit.CustomConnectorStart.XYZ))
					{
						xyz3 -= (xyz3 - xyz4).Normalize() * num;
						xyz4 -= (xyz4 - xyz3).Normalize() * num;
					}
					else
					{
						xyz3 += (xyz3 - xyz4).Normalize() * num;
						xyz4 += (xyz4 - xyz3).Normalize() * num;
					}
				}
				if (hyconduit.IsPointOnDuctCurve(xyz3) && hyconduit.IsPointOnDuctCurve(xyz4))
				{
					new HYCableTrayRoundBeam().NewPipePIANYI(conduit, xyz3, xyz4, form.distance, form.angle, form.SJORPY, false);
					return;
				}
				YJKMessageBox.Show("切割点不在该线管上，无法复制升降或偏移！");
				return;
			}
			else if (doc.GetElement(reference) is CableTray)
			{
				CableTray cableTray = doc.GetElement(reference) as CableTray;
				HYCableTray hycableTray = new HYCableTray(cableTray);
				YJKLine hyline3 = new YJKLine(hycableTray.CustomConnectorStart.XYZ, hycableTray.CustomConnectorEnd.XYZ);
				XYZ xyz5 = hycableTray.GetProject(xyz1, true);
				XYZ xyz6 = hycableTray.GetProject(xyz2, true);
				if (!hyline.IsParallel(hyline3.Line))
				{
					YJKMessageBox.Show("该桥架不平行！");
					return;
				}
				if (form.SJORPY == "偏移")
				{
					HYCreatePipeHelper hycreatePipeHelper2 = new HYCreatePipeHelper();
					form.distance = ddd;
					double num2 = hyline.Line.Distance(xyz5);
					if (ddd > 0.0)
					{
						if (hycreatePipeHelper2.judgePointIsAnotherLine(hyline.Line, hycableTray.CustomConnectorStart.XYZ))
						{
							xyz5 += (xyz5 - xyz6).Normalize() * num2;
							xyz6 += (xyz6 - xyz5).Normalize() * num2;
						}
						else
						{
							xyz5 -= (xyz5 - xyz6).Normalize() * num2;
							xyz6 -= (xyz6 - xyz5).Normalize() * num2;
						}
					}
					else if (hycreatePipeHelper2.judgePointIsAnotherLine(hyline.Line, hycableTray.CustomConnectorStart.XYZ))
					{
						xyz5 -= (xyz5 - xyz6).Normalize() * num2;
						xyz6 -= (xyz6 - xyz5).Normalize() * num2;
					}
					else
					{
						xyz5 += (xyz5 - xyz6).Normalize() * num2;
						xyz6 += (xyz6 - xyz5).Normalize() * num2;
					}
				}
				if (hycableTray.IsPointOnDuctCurve(xyz5) && hycableTray.IsPointOnDuctCurve(xyz6))
				{
					new HYCableTrayRoundBeam().NewPipePIANYI(cableTray, xyz5, xyz6, form.distance, form.angle, form.SJORPY, false);
					return;
				}
				YJKMessageBox.Show("切割点不在该桥架上，无法复制升降或偏移！");
				return;
			}
			else
			{
				if (!(doc.GetElement(reference) is Duct))
				{
					if (doc.GetElement(reference) is Pipe)
					{
						Pipe pipe = doc.GetElement(reference) as Pipe;
						YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
						YJKLine hyline4 = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
						XYZ xyz7 = hypiping.GetProject(xyz1, true);
						XYZ xyz8 = hypiping.GetProject(xyz2, true);
						if (hyline.IsParallel(hyline4.Line))
						{
							if (form.SJORPY == "偏移")
							{
								HYCreatePipeHelper hycreatePipeHelper3 = new HYCreatePipeHelper();
								form.distance = ddd;
								double num3 = hyline.Line.Distance(xyz7);
								if (ddd > 0.0)
								{
									if (hycreatePipeHelper3.judgePointIsAnotherLine(hyline.Line, hypiping.StartConnector.Origin))
									{
										xyz7 += (xyz7 - xyz8).Normalize() * num3;
										xyz8 += (xyz8 - xyz7).Normalize() * num3;
									}
									else
									{
										xyz7 -= (xyz7 - xyz8).Normalize() * num3;
										xyz8 -= (xyz8 - xyz7).Normalize() * num3;
									}
								}
								else if (hycreatePipeHelper3.judgePointIsAnotherLine(hyline.Line, hypiping.StartConnector.Origin))
								{
									xyz7 -= (xyz7 - xyz8).Normalize() * num3;
									xyz8 -= (xyz8 - xyz7).Normalize() * num3;
								}
								else
								{
									xyz7 += (xyz7 - xyz8).Normalize() * num3;
									xyz8 += (xyz8 - xyz7).Normalize() * num3;
								}
							}
							if (hypiping.IsPointOnPipingCurve(xyz7) && hypiping.IsPointOnPipingCurve(xyz8))
							{
								new HYPipeConnect().NewPipePIANYI(pipe, xyz7, xyz8, form.distance, form.angle, form.SJORPY, false);
								return;
							}
							YJKMessageBox.Show("切割点不在该管道上，无法复制升降或偏移！");
							return;
						}
						else
						{
							YJKMessageBox.Show("该管道不平行！");
						}
					}
					return;
				}
				Duct duct = doc.GetElement(reference) as Duct;
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct);
				YJKLine hyline5 = new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
				XYZ xyz9 = hypiping2.GetProject(xyz1, true);
				XYZ xyz10 = hypiping2.GetProject(xyz2, true);
				if (!hyline.IsParallel(hyline5.Line))
				{
					YJKMessageBox.Show("该风管不平行！");
					return;
				}
				if (form.SJORPY == "偏移")
				{
					HYCreatePipeHelper hycreatePipeHelper4 = new HYCreatePipeHelper();
					form.distance = ddd;
					double num4 = hyline.Line.Distance(xyz9);
					if (ddd > 0.0)
					{
						if (hycreatePipeHelper4.judgePointIsAnotherLine(hyline.Line, hypiping2.StartConnector.Origin))
						{
							xyz9 += (xyz9 - xyz10).Normalize() * num4;
							xyz10 += (xyz10 - xyz9).Normalize() * num4;
						}
						else
						{
							xyz9 -= (xyz9 - xyz10).Normalize() * num4;
							xyz10 -= (xyz10 - xyz9).Normalize() * num4;
						}
					}
					else if (hycreatePipeHelper4.judgePointIsAnotherLine(hyline.Line, hypiping2.StartConnector.Origin))
					{
						xyz9 -= (xyz9 - xyz10).Normalize() * num4;
						xyz10 -= (xyz10 - xyz9).Normalize() * num4;
					}
					else
					{
						xyz9 += (xyz9 - xyz10).Normalize() * num4;
						xyz10 += (xyz10 - xyz9).Normalize() * num4;
					}
				}
				if (hypiping2.IsPointOnPipingCurve(xyz9) && hypiping2.IsPointOnPipingCurve(xyz10))
				{
					new HYDuctAlignment().NewPipePIANYI(duct, xyz9, xyz10, form.distance, form.angle, form.SJORPY, false);
					return;
				}
				YJKMessageBox.Show("切割点不在该风管上，无法复制升降或偏移！");
				return;
			}
		}

		private void conduitPYSJ(Reference myRef, FormRoundBeamUseHand form, Document doc, Selection sel, ISelectionFilter PipeFilter)
		{
			double distance = form.distance;
			Conduit conduit = doc.GetElement(myRef) as Conduit;
			HYConduit hyconduit = new HYConduit(conduit);
			XYZ globalPoint = myRef.GlobalPoint;
			if (form.model == "单侧")
			{
				YJKLine hyline = new YJKLine(hyconduit.StartXYZ, hyconduit.EndXYZ);
				XYZ xyz = null;
				for (;;)
				{
					xyz = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, PipeFilter, "请在线管上选择一点确定升降或者偏移的方向").GlobalPoint;
					if (new XYZ(xyz.X, xyz.Y, globalPoint.Z).DistanceTo(globalPoint) >= 0.065616797900262466)
					{
						break;
					}
					YJKMessageBox.Show("选择的方向点与切割位置点过于接近，无法判断方向，请重新选择方向点！");
				}
				if (new HYCableTrayRoundBeam().NewPipePIANYI(conduit, globalPoint, form.distance, form.angle, form.SJORPY, xyz))
				{
					for (;;)
					{
						try
						{
							this.danceXUNHUAN(distance, hyline, globalPoint, xyz, form, doc, sel);
							continue;
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
						}
						break;
					}
				}
			}
			if (form.model == "两侧")
			{
				XYZ xyz2 = null;
				Reference reference;
				for (;;)
				{
					reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, PipeFilter, "请选择要升降或者偏移的线管,点击点为切割位置点二");
					xyz2 = reference.GlobalPoint;
					if (xyz2.DistanceTo(globalPoint) >= 0.065616797900262466)
					{
						break;
					}
					YJKMessageBox.Show("选择的切割位置点二与首个切割位置点过于接近，请重新选择切割位置点二！");
				}
				Conduit conduit2 = doc.GetElement(reference) as Conduit;
				HYConduit hyconduit2 = new HYConduit(conduit2);
				YJKLine hyline2 = new YJKLine(hyconduit2.CustomConnectorStart.XYZ, hyconduit2.CustomConnectorEnd.XYZ);
				HYCableTrayRoundBeam hycableTrayRoundBeam = new HYCableTrayRoundBeam();
				if (conduit.Id == conduit2.Id)
				{
					if (!hycableTrayRoundBeam.NewPipePIANYI(conduit, globalPoint, xyz2, form.distance, form.angle, form.SJORPY, false))
					{
						return;
					}
					for (;;)
					{
						try
						{
							this.liangceXUNHUAN(distance, hyline2, globalPoint, xyz2, form, doc, sel);
							continue;
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
							return;
						}
						break;
					}
				}
				if (conduit.Id != conduit2.Id)
				{
					YJKMessageBox.Show("线管升降不支持带附件的升降！");
				}
			}
		}

		private void cableTrayPYSJ(Reference myRef, FormRoundBeamUseHand form, Document doc, Selection sel, ISelectionFilter PipeFilter)
		{
			double distance = form.distance;
			CableTray cableTray = doc.GetElement(myRef) as CableTray;
			HYCableTray hycableTray = new HYCableTray(cableTray);
			XYZ globalPoint = myRef.GlobalPoint;
			if (form.model == "单侧")
			{
				YJKLine hyline = new YJKLine(hycableTray.StartXYZ, hycableTray.EndXYZ);
				XYZ xyz = null;
				for (;;)
				{
					xyz = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, PipeFilter, "请在桥架上选择一点确定升降或者偏移的方向").GlobalPoint;
					if (new XYZ(xyz.X, xyz.Y, globalPoint.Z).DistanceTo(globalPoint) >= 0.065616797900262466)
					{
						break;
					}
					YJKMessageBox.Show("选择的方向点与切割位置点过于接近，无法判断方向，请重新选择方向点！");
				}
				if (new HYCableTrayRoundBeam().NewPipePIANYI(cableTray, globalPoint, form.distance, form.angle, form.SJORPY, xyz))
				{
					for (;;)
					{
						try
						{
							this.danceXUNHUAN(distance, hyline, globalPoint, xyz, form, doc, sel);
							continue;
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
						}
						break;
					}
				}
			}
			if (form.model == "两侧")
			{
				XYZ xyz2 = null;
				Reference reference;
				for (;;)
				{
					reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, PipeFilter, "请选择要升降或者偏移的桥架,点击点为切割位置点二");
					xyz2 = reference.GlobalPoint;
					if (xyz2.DistanceTo(globalPoint) >= 0.065616797900262466)
					{
						break;
					}
					YJKMessageBox.Show("选择的切割位置点二与首个切割位置点过于接近，请重新选择切割位置点二！");
				}
				CableTray cableTray2 = doc.GetElement(reference) as CableTray;
				HYCableTray hycableTray2 = new HYCableTray(cableTray2);
				YJKLine hyline2 = new YJKLine(hycableTray2.CustomConnectorStart.XYZ, hycableTray2.CustomConnectorEnd.XYZ);
				HYCableTrayRoundBeam hycableTrayRoundBeam = new HYCableTrayRoundBeam();
				if (cableTray.Id == cableTray2.Id)
				{
					if (!hycableTrayRoundBeam.NewPipePIANYI(cableTray, globalPoint, xyz2, form.distance, form.angle, form.SJORPY, false))
					{
						return;
					}
					for (;;)
					{
						try
						{
							this.liangceXUNHUAN(distance, hyline2, globalPoint, xyz2, form, doc, sel);
							continue;
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
							return;
						}
						break;
					}
				}
				if (cableTray.Id != cableTray2.Id)
				{
					YJKMessageBox.Show("桥架升降不支持带附件的升降！");
				}
			}
		}

		private void ductPYSJ(Reference myRef, FormRoundBeamUseHand form, Document doc, Selection sel, ISelectionFilter PipeFilter)
		{
			double distance = form.distance;
			Duct duct = doc.GetElement(myRef) as Duct;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			XYZ globalPoint = myRef.GlobalPoint;
			if (form.model == "单侧")
			{
				YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				XYZ xyz = null;
				for (;;)
				{
					xyz = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, PipeFilter, "请在风管上选择一点确定升降或者偏移的方向").GlobalPoint;
					if (new XYZ(xyz.X, xyz.Y, globalPoint.Z).DistanceTo(globalPoint) >= 0.065616797900262466)
					{
						break;
					}
					YJKMessageBox.Show("选择的方向点与切割位置点过于接近，无法判断方向，请重新选择方向点！");
				}
				if (new HYDuctAlignment().NewPipePIANYI(duct, globalPoint, form.distance, form.angle, form.SJORPY, xyz))
				{
					for (;;)
					{
						try
						{
							this.danceXUNHUAN(distance, hyline, globalPoint, xyz, form, doc, sel);
							continue;
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
						}
						break;
					}
				}
			}
			if (form.model == "两侧")
			{
				XYZ xyz2 = null;
				Reference reference;
				for (;;)
				{
					reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, PipeFilter, "请选择要升降或者偏移的风管,点击点为切割位置点二");
					xyz2 = reference.GlobalPoint;
					if (xyz2.DistanceTo(globalPoint) >= 0.065616797900262466)
					{
						break;
					}
					YJKMessageBox.Show("选择的切割位置点二与首个切割位置点过于接近，请重新选择切割位置点二！");
				}
				Duct duct2 = doc.GetElement(reference) as Duct;
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
				YJKLine hyline2 = new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
				HYDuctAlignment hyductAlignment = new HYDuctAlignment();
				if (duct.Id == duct2.Id)
				{
					if (!hyductAlignment.NewPipePIANYI(duct, globalPoint, xyz2, form.distance, form.angle, form.SJORPY, false))
					{
						return;
					}
					for (;;)
					{
						try
						{
							this.liangceXUNHUAN(distance, hyline2, globalPoint, xyz2, form, doc, sel);
							continue;
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
							return;
						}
						break;
					}
				}
				if (duct.Id != duct2.Id)
				{
					hyductAlignment.NewPipePIANYI(duct, globalPoint, duct2, xyz2, form.distance, form.angle, form.SJORPY);
				}
			}
		}

		private void pipePYSJ(Reference myRef, FormRoundBeamUseHand form, Document doc, Selection sel, ISelectionFilter PipeFilter)
		{
			double distance = form.distance;
			Pipe pipe = doc.GetElement(myRef) as Pipe;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ globalPoint = myRef.GlobalPoint;
			if (form.model == "单侧")
			{
				YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
				XYZ xyz = null;
				for (;;)
				{
					xyz = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, PipeFilter, "请在管道上选择一点确定升降或者偏移的方向").GlobalPoint;
					if (new XYZ(xyz.X, xyz.Y, globalPoint.Z).DistanceTo(globalPoint) >= 0.065616797900262466)
					{
						break;
					}
					YJKMessageBox.Show("选择的方向点与切割位置点过于接近，无法判断方向，请重新选择方向点！");
				}
				if (new HYPipeConnect().NewPipePIANYI(pipe, globalPoint, form.distance, form.angle, form.SJORPY, xyz))
				{
					for (;;)
					{
						try
						{
							this.danceXUNHUAN(distance, hyline, globalPoint, xyz, form, doc, sel);
							continue;
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
						}
						break;
					}
				}
			}
			if (form.model == "两侧")
			{
				XYZ xyz2 = null;
				Reference reference;
				for (;;)
				{
					reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, PipeFilter, "请选择要升降或者偏移的管道,点击点为切割位置点二");
					xyz2 = reference.GlobalPoint;
					if (xyz2.DistanceTo(globalPoint) >= 0.065616797900262466)
					{
						break;
					}
					YJKMessageBox.Show("选择的切割位置点二与首个切割位置点过于接近，请重新选择切割位置点二！");
				}
				Pipe pipe2 = doc.GetElement(reference) as Pipe;
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe2);
				YJKLine hyline2 = new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
				HYPipeConnect hypipeConnect = new HYPipeConnect();
				if (pipe.Id == pipe2.Id)
				{
					if (!hypipeConnect.NewPipePIANYI(pipe, globalPoint, xyz2, form.distance, form.angle, form.SJORPY, false))
					{
						return;
					}
					for (;;)
					{
						try
						{
							this.liangceXUNHUAN(distance, hyline2, globalPoint, xyz2, form, doc, sel);
							continue;
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
							return;
						}
						break;
					}
				}
				if (pipe.Id != pipe2.Id)
				{
					hypipeConnect.NewPipePIANYI(pipe, globalPoint, pipe2, xyz2, form.distance, form.angle, form.SJORPY);
				}
			}
		}
	}
}
